#!/usr/bin/env python
# -*- coding: utf-8 -*-


from odoo import api, fields, models, _

class PurchaseOrder(models.Model):
    _inherit = 'purchase.order'

    def button_approve(self, force=False):
        result = super().button_approve(force=force)
        for row in self:
            if not self.env.ref('sb_iqc.stock_location_inspection'):
                continue
            elif not row.picking_ids.move_ids.filtered(lambda x:not x.product_id.requires_iqc):
                row.picking_ids.location_dest_id = self.env.ref('sb_iqc.stock_location_inspection').id
            elif row.picking_ids.move_ids.filtered(lambda x:not x.product_id.requires_iqc):
                for c_row in row.picking_ids.move_ids.filtered(lambda x:not x.product_id.requires_iqc).picking_id.check_ids:
                    c_row.split_no_quality_check_stock_move()
        return result
